/**
 * 
 */
package com.teach.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author Liulianqiu
 *2020年7月18日
 */
public class JDBCutils {

//	获取连接方法
	public static Connection getConnection() throws Exception {
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
		Properties pro = new Properties();
		pro.load(in);
		
		String url = pro.getProperty("url");
		String user = pro.getProperty("user");
		String password = pro.getProperty("password");
		String driverClass = pro.getProperty("driverClass");
		Class.forName(driverClass);
		
		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}
	
//	关闭连接方法
	public static void closeConnection(Connection conn,Statement ps) {
		try {
			if(conn != null)
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(ps != null)
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
//	关闭所有资源
	public static void closeResources(Connection conn,Statement ps,ResultSet rs) {
		try {
			if(conn != null)
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(ps != null)
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}try {
			if(rs != null)
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
